<?php
/** require:
 *  __PHY_LIB
 */
require_once(__PHY_LIB."/class/Item.class.php");
require_once(__PHY_LIB."/func/gd.func.php");

class User extends Item {
/// definition and constructor ///
	function User($id=0, $field="") {
		Query::Query();
		$this->cfgKeyField("id");
		$this->cfgAllField("id,email,nick,pswd,time_added,sign,identity,gender,photo_s,photo_m,birthday,phone,mobile,address,hometown,city,postcode,remark");
		$this->cfgTable("unit_user");
		if ("" == $field || "*" == $field) {
			$this->cfgField($this->mAllField);
		}
		else {
			$this->cfgField($field);
		}
		if (0 < $id) {
			$this->cfgWhere($this->mKeyField . "=" . $id);
			$this->load();
		}
		else {
			$this->set($this->mKeyField, $id);
		}
	}

	function loadByEmail($sEmail) {
		$this->cfgWhere("email='".addslashes($sEmail)."'");
		$this->load();
	}

	function pswdEncode($sPswd) {
		return md5('txh'.$sPswd);
	}

	function invalid() {
		if (!strpos($this->get('email'), '@')) {
			return 'email';
		}

		return false;
	}

	function logout() {
		setcookie('u_type', '', time()-86400, '/'); unset($_COOKIE['u_type']);
		setcookie('u_id',   '', time()-86400, '/'); unset($_COOKIE['u_id']);
		setcookie('u_nick', '', time()-86400, '/'); unset($_COOKIE['u_nick']);
		return true;
	}

	function login($sAcct, $sPswd, $bLong=false) {
		if (!$sAcct || !$sPswd) { return false; }
		$oUser = new User();
		$oUser->loadByEmail($sAcct);
		if ($oUser->pswdEncode($sPswd) == $oUser->get('pswd')) {
			$nExpire = $bLong ? (time() + 86400*30) : null; // 30天cookie过期
			setcookie('u_type', 'user',              $nExpire, '/'); $_COOKIE['u_type'] = 'user';
			setcookie('u_id',   $oUser->getID(),     $nExpire, '/'); $_COOKIE['u_id'] = $oUser->getID();
			setcookie('u_nick', $oUser->get('nick'), $nExpire, '/'); $_COOKIE['u_nick'] = $oUser->get('nick');
			return true;
		}
		else {
			return false;
		}
	}

	function calcFileUrl($sField) {
		if (!(0 < $this->getID())) { return false; }
		$sNumber = str_pad($this->getID(), 9, '0', STR_PAD_LEFT);
		$nStamp = date('YmdHis');
		if ('photo_s' == $sField) {
			$sUrl = "/user/".substr($sNumber, 0, 3)."/".substr($sNumber, 3, 3)."/".substr($sNumber, 6, 3)."/photo/s_{$nStamp}.png";
		}
		elseif ('photo_m' == $sField) {
			$sUrl = "/user/".substr($sNumber, 0, 3)."/".substr($sNumber, 3, 3)."/".substr($sNumber, 6, 3)."/photo/m_{$nStamp}.png";
		}
		else {
			return false;
		}
		return $sUrl;
	}
	function setFile($sField, $sSrcPath) {
		$sUrl = $this->calcFileUrl($sField);
		if (!$sUrl) { return false; }
		mkdir_plus(dirname(__PHY_STO.$sUrl));
		if (move_uploaded_file($sSrcPath, __PHY_STO.$sUrl)) {
			$this->set($sField, $sUrl);
			return true;
		}
		else {
			return false;
		}
	}
	function setPhoto($sSrcPath) {
		$sUrl_s = $this->calcFileUrl('photo_s');
		if (!$sUrl_s) { return false; }
		$sUrl_m = $this->calcFileUrl('photo_m');
		if (!$sUrl_m) { return false; }
		mkdir_plus(dirname(__PHY_STO.$sUrl_s));
		mkdir_plus(dirname(__PHY_STO.$sUrl_m));
		/// 得到图片信息 ///
		list($nOldW, $nOldH, $nType) = getimagesize($sSrcPath);
		if (IMAGETYPE_GIF == $nType) {
			$hOld = imagecreatefromgif($sSrcPath);
		}
		elseif (IMAGETYPE_JPEG == $nType) {
			$hOld = imagecreatefromjpeg($sSrcPath);
		}
		elseif (IMAGETYPE_PNG == $nType) {
			$hOld = imagecreatefrompng($sSrcPath);
		}
		else { return false; } // 只支持gif/jpg/png
		/// 小图片 ///
		$nNewW = 50; $nNewH = 50;
		$hNew = imagecreatetruecolor($nNewW, $nNewH);
		$cWhite = imagecolorallocate($hNew, 255, 255, 255);
		imagefilledrectangle($hNew, 0, 0, $nNewW, $nNewH, $cWhite); // 画一个白底
		list($nFillW, $nFillH) = gd_zoom_out(array($nOldW, $nOldH), array($nNewW, $nNewH));
		imagecopyresampled($hNew, $hOld,
			floor(($nNewW - $nFillW)/2), floor(($nNewH - $nFillH)/2), 0, 0,
			$nFillW, $nFillH, $nOldW, $nOldH);
		imagepng($hNew, __PHY_STO.$sUrl_s); // 保存
		imagedestroy($hNew);
		$this->set('photo_s', $sUrl_s);
		/// 中图片 ///
		$nNewW = 100; $nNewH = 100;
		$hNew = imagecreatetruecolor($nNewW, $nNewH);
		$cWhite = imagecolorallocate($hNew, 255, 255, 255);
		imagefilledrectangle($hNew, 0, 0, $nNewW, $nNewH, $cWhite); // 画一个白底
		list($nFillW, $nFillH) = gd_zoom_out(array($nOldW, $nOldH), array($nNewW, $nNewH));
		imagecopyresampled($hNew, $hOld,
			floor(($nNewW - $nFillW)/2), floor(($nNewH - $nFillH)/2), 0, 0,
			$nFillW, $nFillH, $nOldW, $nOldH);
		imagepng($hNew, __PHY_STO.$sUrl_m); // 保存
		imagedestroy($hNew);
		$this->set('photo_m', $sUrl_m);

		return true;
	}

} /// end class User.


class UserList extends ItemList {
	/// definition and constructor ///
	function UserList($tablename=null) {
		ItemList::ItemList();
		$this->cfgClass("User");
		$this->cfgKeyField("id");
		$this->cfgAllField("id,email,nick,pswd,time_added,sign,identity,gender,photo_s,photo_m,birthday,phone,mobile,address,hometown,city,postcode,remark");
		$this->cfgTable(null == $tablename ? "unit_user" : $tablename);
	}

} /// end class UserList.
?>
